package org.jeecg.common.bdPay;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.constant.CommonConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * rest发送请求工具类
 *
 * @author chechen
 * @date 2022-05-07
 */
@Slf4j
@Service
public class RestTemplateUtil {

    private RestTemplate restTemplate;

    @Autowired
    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    /**
     * 字节小程序post请求
     */
    public String byteDancePostRequest(JSONObject jsonObject, String url) {
        String result = "";
        try {
            HttpHeaders headers = new HttpHeaders();
            // 所有的请求需要用JSON格式发送
            headers.setContentType(MediaType.parseMediaType(CommonConstant.APPLICATION_JSON_UTF8_VALUE));
            HttpEntity<Object> formEntity = new HttpEntity<>(jsonObject, headers);
            result = restTemplate.postForObject(url, formEntity, String.class);
        } catch (Exception e) {
            log.error("抖音小程序post请求异常{}", url);
            e.printStackTrace();
        }
        return result;
    }

}